﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1
{
    public class CaShareSignTool
    {
        /// <summary>
        /// 启动ca互认签章工具
        /// </summary>
        public void Start()
        {
            Process process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.Arguments = "CAShareDetectInstallTool://";
            process.StartInfo.FileName = @"explorer";
            process.Start();
            Task.Delay(TimeSpan.FromSeconds(2));
        }

        public async Task WebSocket(List<WaitSignFile> waitSignFiles, SealInfo sealInfo, CaShareConfig caShareConfig,string callbackUrl)
        {
            try
            {
                //请求数据封装
                var msgJson = new
                {
                    CAShareUseScene = "00",
                    keyParameter = new
                    {
                        document = waitSignFiles,
                        seal = sealInfo,
                        caShareComponentConfig = caShareConfig,
                        tradingSystemConfig=new
                        {
                            callbackRequestUrl = callbackUrl
                        }
                    }
                };
                var msgStr = JsonConvert.SerializeObject(msgJson);

                ClientWebSocket webSocket = new ClientWebSocket();
                CancellationToken cancellation = new CancellationToken();
                var url = "ws://localhost:12345";
                await webSocket.ConnectAsync(new Uri(url), cancellation);
                var bsend = Encoding.UTF8.GetBytes(msgStr);
                //var arraySegment = new ArraySegment<byte>(bsend);
                await webSocket.SendAsync(bsend, WebSocketMessageType.Text, true, cancellation);

            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }
    }

    public class WaitSignFile
    {
        /// <summary>
        /// 待签章文件路径
        /// </summary>
        [JsonProperty("awaitingSignatureFilePath")]
        public string AwaitingSignatureFilePath { get; set; }
        /// <summary>
        /// 待签章文件名称
        /// </summary>
        [JsonProperty("awaitingSignatureFileName")]
        public string AwaitingSignatureFileName { get; set; }
        /// <summary>
        /// 待签章文件类型
        /// </summary>

        [JsonProperty("awaitingSignatureFileType")]
        public string AwaitingSignatureFileType { get; set; }

        /// <summary>
        /// 签章后文件的保存路径
        /// </summary>
        [JsonProperty("signatureFileSavePath")]
        public string SignatureFileSavePath { get; set; }
        /// <summary>
        /// 签章后文件名称
        /// </summary>

        [JsonProperty("signatureFileName")]
        public string SignatureFileName { get; set; }

    }
    public class SealInfo
    {
        /// <summary>
        /// 印章图片dpi
        /// </summary>
        [JsonProperty("dpi")]
        public int Dpi { get; set; }
        /// <summary>
        /// 印章透明度，默认 0.8
        /// </summary>
        [JsonProperty("transparency")]
        public float Transparency { get; set; }
        /// <summary>
        /// 印章图片最大宽高，默认 150
        /// </summary>
        [JsonProperty("sealImgMaxValue")]
        public int SealImgMaxValue { get; set; }
    }
}
